package com.hu.util;

import org.apache.commons.lang3.time.FastDateFormat;

import java.text.ParseException;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;

public class DateFormatUtils extends org.apache.commons.lang3.time.DateUtils {
    public static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String DATETIME_NUMBER_FORMAT = "yyyyMMddHHmmss";
    public static final String DATE_FORMAT = "yyyyMMdd";
    public static final String DAY_FORMAT = "yyyy-MM-dd";
    public static final String HOUR_FORMAT = "yyyyMMddHH";
    public static final String MINUTE_FORMAT = "yyyyMMddHHmm";

    public static String toTimeFormat(LocalTime time, String timeFormat) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(timeFormat);
        return time.format(formatter);
    }

    public static String toDateTimeFormat(LocalDateTime datetime) {
        return toDateTimeFormat(datetime, DATETIME_FORMAT);
    }

    public static String toDateTimeFormat(LocalDateTime datetime, String dateTimeFormat) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimeFormat);
        return datetime.format(formatter);
    }

    public static String unixToDateTimeFormat(long unixTime) {
        return unixToDateTimeFormat(unixTime, DATETIME_FORMAT, false);
    }

    public static String unixToDateTimeFormat(long unixTime, String dateTimeFormat) {
        return unixToDateTimeFormat(unixTime, dateTimeFormat, true);
    }

    public static String unixToDateTimeFormat(long unixTime, String dateTimeFormat, boolean isSecond) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern(dateTimeFormat);
        Instant instant;
        if (isSecond) {
            instant = Instant.ofEpochSecond(unixTime);
        } else {
            instant = Instant.ofEpochMilli(unixTime);
        }
        ZoneId zoneId = ZoneId.systemDefault();
        String dateStr = LocalDateTime.ofInstant(instant, zoneId).format(formatter);
        return dateStr;
    }

    public static Date parseByDayPattern(String str) {
        return parseDate(str, DAY_FORMAT);
    }

    public static Date parseDate(String str, String pattern) {
        try {
            return DateFormatUtils.parseDate(str, new String[]{pattern});
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    public static long getTimeMillis(String time) {
        Date input = parseDate(toDateTimeFormat(LocalDateTime.now(), DAY_FORMAT) + " " + time, DATETIME_FORMAT);
        return input.getTime();
    }

    public static String addDays(Date date, int amount, String pattern) {
        Date addDate = DateFormatUtils.addDays(date, amount);
        return FastDateFormat.getInstance(pattern).format(addDate);
    }

    public static void main(String[] args) {
        long x = Long.MIN_VALUE;
        System.out.println(parseDate("20190701170816", DATETIME_NUMBER_FORMAT).getTime() / 1000);
        System.out.println(unixToDateTimeFormat(System.currentTimeMillis(), DATETIME_FORMAT, false));
    }
}
